<template>
	<el-dialog title="帖子信息" :visible.sync="dialogFormVisible" :before-close="cleardata">
		<el-form :model="form">
			<el-form-item label="标题" :label-width="formLabelWidth">
				<el-input v-model="form.postName" autocomplete="off" style="width: 40%;"></el-input>
			</el-form-item>
			<el-form-item label="帖子封面" :label-width="formLabelWidth" prop="contentCover">
				<img v-for="(image, index) in form.contentCover.split(',')" :key="index" :src="$store.state.imgShowRoad2 + '/file/' + image" class="avatar" />
				<el-upload class="orders-img_el_upload" show-file-list :action="upurl" :limit="limitCountImg"
					list-type="picture-card" :on-change="handleChange" :on-preview="handlePreview"
					:on-remove="handleRemove" :on-success="handleSuccess" :on-error="handleError" :file-list="fileList"
					:class="{ disUoloadSty: noneBtnImg }" ref="upload" :disabled="uploadDisabled"
					@click.stop="submitUpload">
					<span class="orders-img_el_upload_btn">添加图片(最多三张)</span>
				</el-upload>
			</el-form-item>
			<el-form-item label="帖子内容" :label-width="formLabelWidth">
				<div ref="editor"></div>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button @click="cleardata">取 消</el-button>
			<el-button type="primary" @click="editMyPost">确 定</el-button>
		</div>
	</el-dialog>
</template>

<script>
	import PostsApi from "@/api/posts.js"
	import Editor from "wangeditor"
	export default {
		components: {
			
		},
		data() {
			return {
				editor: null,
				imgurl: "",
				dialogFormVisible: false,
				form: {
					postId: "",
					postName: "",
					contentCover: "",
					postContent: "",
				},
				formLabelWidth: '120px',
				uploadDisabled: false,
				noneBtnImg: false,
				limitCountImg: 3,
				upurl: "/api/file/uploadList/posts",
				fileList: [],
			};
		},
		methods: {
			//创建富文本容器
			createE() {
				this.$nextTick(() => {
					this.editor = new Editor(this.$refs.editor)
					this.editor.config.uploadImgServer = "api/file/editUpload/posts"
					this.editor.config.uploadFileName = "file"
					this.editor.config.uploadVideoServer = 'api/file/editVideoUpload/posts'
					this.editor.config.uploadVideoName = "file"
					this.editor.create()
					this.editor.txt.html(this.form.postContent)
				})
			},
			cleardata() {
				this.dialogFormVisible = false;
				this.editor.destroy();
			},
			async editMyPost() {
				let content = this.editor.txt.html();
				const res = await PostsApi.editMyPost("/znExchangePost/editMyPost", {
					postId: this.form.postId,
					postName: this.form.postName,
					contentCover: this.form.contentCover,
					postContent: this.form.postContent = content,
				});
				if (res.code == 200) {
					this.cleardata();
					this.$message.success("编辑成功");
					this.$emit('getMyIssuePost');
				} else {
					this.$message.warning("失败");
				}
			},
			handleError(err) {
				console.error("上传错误:", err);
				this.$message.error("上传失败，请重试！");
			},
			handleSuccess(response, file, fileList) {
				if (response.flag === true) {
					this.fileList = fileList;
					// 改为将所有文件名保存到数组中
					this.form.contentCover = fileList.map(file => file.response.data).join(",");
					if (fileList.length > 3) {
						this.uploadDisabled = true;
					}
					this.$message.success(response.message);
				} else {
					this.$message.error(response.message);
				}
			},
			handleChange(file, fileList) {
				this.noneBtnImg = fileList.length >= this.limitCountImg;
			},
			handleRemove(file, fileList) {
				this.noneBtnImg = fileList.length >= this.limitCountImg;
				this.fileList.pop();
				this.uploadDisabled = false;
			},
			handlePreview(file) {
				this.dialogImageUrl = file.url;
				this.dialogVisible = true;
			},
			submitUpload() {
				this.$refs.upload.submit();
			},
		},
	};
</script>

<style lang="scss" scoped>
	.avatar {
		float: left;
		width: 148px;
		height: 148px;
		border-radius: 3%;
		margin-right: 10px;
	}
</style>